package com.course.web.biz;

import com.course.domain.enums.TaskType;
import com.course.domain.model.SpecInfo;
import com.course.domain.model.SpecWeek;
import com.course.domain.service.SpecService;
import com.course.web.model.SpecInfoVM;
import com.course.web.model.SpecWeekVM;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by Justin on 2017/6/6.
 */
@Component
public class WebSpecBiz {

    private final SpecService specService;

    @Autowired
    public WebSpecBiz(SpecService specService) {
        this.specService = specService;
    }

    public SpecInfoVM createInfo(SpecInfoVM vm) {
        SpecInfo specInfo = specService.createInfo(toInfo(vm));
        vm.setId(specInfo.getId());
        return vm;
    }

    public SpecInfoVM updateInfo(SpecInfoVM vm) {
        specService.updateInfo(toInfo(vm));
        return vm;
    }

    public SpecInfoVM deleteInfo(SpecInfoVM vm) {
        specService.deleteInfo(toInfo(vm));
        return vm;
    }

    public void saveWeek(List<SpecWeekVM> current,
                         List<SpecWeekVM> origins) {
        specService.deleteWeek(Lists.transform(origins, this::toWeek));
        specService.createWeek(Lists.transform(current, this::toWeek));
    }

    public SpecInfoVM loadInfo(int specId) {
        SpecInfo specInfo = specService.loadInfo(specId);
        return transform(specInfo);
    }

    public List<SpecInfoVM> listInfo() {
        List<SpecInfo> infoList = specService.listInfo();
        return Lists.transform(infoList, this::transform);
    }

    public List<SpecInfoVM> listInfoPublished() {
        List<SpecInfo> infoList = specService.listInfoPublished();
        return Lists.transform(infoList, this::transform);
    }

    public List<SpecWeekVM> listWeek(int specId) {
        List<SpecWeek> weekList = specService.findWeek(specId);
        return Lists.transform(weekList, this::transform);
    }

    private SpecInfoVM transform(SpecInfo specInfo) {
        SpecInfoVM vm = new SpecInfoVM();
        vm.setId(specInfo.getId());
        vm.setTitle(specInfo.getTitle());
        vm.setParts(specInfo.getParts());
        vm.setLocked(specInfo.getState() == 1);
        return vm;
    }

    private SpecWeekVM transform(SpecWeek specWeek) {
        SpecWeekVM vm = new SpecWeekVM();
        vm.setId(specWeek.getId());
        vm.setSpecId(specWeek.getSpecId());
        vm.setPartIdx(specWeek.getPartIdx());
        vm.setWorkday(specWeek.getWorkdayTitle());
        vm.setWeekend(specWeek.getWeekendTitle());
        TaskType[] types = new TaskType[8];
        for (int weekDay = 1; weekDay <= 7; weekDay++) {
            types[weekDay] = specWeek.getByWeekDay(weekDay);
        }
        vm.setTasks(types);
        return vm;
    }

    private SpecInfo toInfo(SpecInfoVM vm) {
        SpecInfo specInfo = new SpecInfo();
        specInfo.setId(vm.getId());
        specInfo.setTitle(vm.getTitle());
        specInfo.setParts(vm.getParts());
        specInfo.setState(vm.isLocked() ? 1 : 0);
        specInfo.setStatus(1);
        return specInfo;
    }

    private SpecWeek toWeek(SpecWeekVM vm) {
        SpecWeek specWeek = new SpecWeek();
        specWeek.setId(vm.getId());
        specWeek.setSpecId(vm.getSpecId());
        specWeek.setPartIdx(vm.getPartIdx());
        specWeek.setWorkdayTitle(vm.getWorkday());
        specWeek.setWeekendTitle(vm.getWeekend());
        for (int weekDay = 1; weekDay <= 7; weekDay++) {
            specWeek.setByWeekDay(weekDay, vm.getTask(weekDay));
        }
        specWeek.setStatus(1);
        return specWeek;
    }
}
